<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\model\Products;
use app\model\ProductsGroup;
use app\model\RefundDetails;
use think\facade\Db;

class Analyse extends BaseController
{
    //新/舊學生消費分析
    public function consume($limit = 15)
    {
        $time = request()->param('time');
        $status = request()->param('status');
        $top = request()->param('top');
        $school = request()->param('school_id');
        $data = \app\model\Student::alias('st')
            ->join('proceeds po', 'st.id=po.student_id', 'left')
            ->join('school sc', 'sc.id=st.school_id', 'left')
            ->field('st.name student_name,st.number,po.money,sc.name')
            ->when($time, function ($query) use ($time) {
                $query->whereBetween('st.create_time', $time);
            })
            ->when($status, function ($query) use ($status) {
                $query->where('st.status', $status);
            })
            ->when($top, function ($query) use ($top) {
                $query->order('po.money', 'desc');
            })
            ->when($school, function ($query) use ($school) {
                $query->order('st.school_id', $school);
            })
            ->paginate($limit);
        return $this->success($data);
    }

//非活躍學生統計表
    public function inactivity($limit=15)
    {
        $type = request()->param('type');
        $status = request()->param('status');
        $school = request()->param('school_id');

        $data = \app\model\Student::alias('st')
            ->join('proceeds po', 'st.id=po.student_id', 'left')
            ->join('student_type ty', 'ty.id=st.student_type_id', 'left')
            ->join('school sc', 'sc.id=st.school_id', 'left')
            ->field('st.name student_name,st.number,st.id,po.create_time,sc.name')
            ->when($type, function ($query) use ($type) {
                $query->where('st.student_type_id', $type);
            })
            ->when($status, function ($query) use ($status) {
                $query->where('st.status', $status);
            })

            ->when($school, function ($query) use ($school) {
                $query->order('st.school_id', $school);
            })
            ->paginate($limit)->toArray();
        foreach ($data['data'] as &$item){
           if(empty($item['create_time'])){
               $item['create_time']= \app\model\Student::where('id',$item['id'])->value('create_time');
               $time=time()-strtotime($item['create_time']);
               $day = $time / (3600 * 24);
               $item['day']=floor($day);
           }
        }
        return $this->success($data);
    }
    //销售排行
    public function sale($limit=15)
    {
        $time = request()->param('time');
        $school = request()->param('school_id');
        $type = request()->param('type');
        $status = request()->param('status');
        $data = \app\model\InvoiceDetails::alias('ind')
            ->join('invoice iv', 'iv.id=ind.invoice_id', 'left')
            ->when($type, function ($query) use ($type) {
                $query->where('ind.type', $type);
            })
            ->when($status, function ($query) use ($status) {
                $query->where('iv.status', $status);
            })
            ->when($school, function ($query) use ($school) {
                $query->where('iv.school_id', $school);
            })
            ->when($time, function ($query) use ($time) {
                $query->whereBetween('ind.create_time', $time);
            })
            ->field('ind.*')
            ->paginate($limit)->toArray();

        foreach ($data['data'] as &$item){
            if($type==1){
                $item['number']=Products::where('id',$item['cid'])->value('number');
            }elseif ($type==2){
                $item['number']=ProductsGroup::where('id',$item['cid'])->value('number');
            }elseif($type==3){
                $item['number']=\app\model\Course::where('id',$item['cid'])->value('number');
            }
        }

            return $this->success($data);
    }


//    public function yearcome($limit=15)
//    {
//
//        $data = [
//            'courseSaleTotal' => 0,
//            'courseRefundTotal' => 0,
//            'productSaleTotal' => 0,
//            'productRefundTotal' => 0,
//            'productComposeSaleTotal' => 0,
//            'productComposeRefundTotal' => 0,
//            'turnoverTotal' => 0,
//            'newStudentTotal' => 0,
//            ['month' => '1月', 'courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
//            ['month' => '2月', 'courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
//            ['month' => '3月', 'courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
//            ['month' => '4月', 'courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
//            ['month' => '5月', 'courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
//            ['month' => '6月', 'courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
//            ['month' => '7月', 'courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
//            ['month' => '8月', 'courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
//            ['month' => '9月', 'courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
//            ['month' => '10月', 'courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
//            ['month' => '11月', 'courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
//            ['month' => '12月', 'courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0]
//        ];
//        $res=\app\model\InvoiceDetails::with(['proceeds'])->select();
//
//        foreach ($res as $item){
//            $month = date('n', strtotime($item->create_time))-1;
//            if($item->type==1){
//                $data[$month]['productSale'] += $item->proceeds->grandtotal;
//                $data[$month]['productRefund'] += RefundDetails::where('cid',$item->cid)->where('type',$item->type)->value('money');
//                $data['productSaleTotal']=20;
//                $data['productRefundTotal']=30;
//            }elseif ($item->type==2){
//                $data[$month]['productComposeSale'] += $item->proceeds->grandtotal;
//                $data[$month]['productComposeRefund'] += RefundDetails::where('cid',$item->cid)->where('type',$item->type)->value('money');
//                $data['productComposeSaleTotal']=20;
//                $data['productComposeRefundTotal']=30;
//            }elseif ($item->type==3){
//                $data[$month]['courseSale'] += $item->proceeds->grandtotal;
//                $data[$month]['courseRefund'] += RefundDetails::where('cid',$item->cid)->where('type',$item->type)->value('money');
//                $data['courseSaleTotal']=20;
//                $data['courseRefundTotal']=30;
//            }
//            $data[$month]['total'] += $item->proceeds->grandtotal ;
//            $data[$month]['total'] =($data[$month]['total']-$data[$month]['productRefund']-$data[$month]['productComposeRefund']-$data[$month]['courseRefund']);
//            $data['turnoverTotal'] += 200;
//        }
//
//        return $this->success($data);
//    }

//    全年營業總表
    public function yearcome()
    {

        $time = request()->param('time');
        $school = request()->param('school_id');
        $data = [
            'courseSaleTotal' => 0,
            'courseRefundTotal' => 0,
            'productSaleTotal' => 0,
            'productRefundTotal' => 0,
            'productComposeSaleTotal' => 0,
            'productComposeRefundTotal' => 0,
            'turnoverTotal' => 0,
            'newStudentTotal' => 0,
        ];

        $monthData = [
            '1月' => ['courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
            '2月' => ['courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
            '3月' => ['courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
            '4月' => ['courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
            '5月' => ['courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
            '6月' => ['courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
            '7月' => ['courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
            '8月' => ['courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
            '9月' => ['courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
            '10月' => ['courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
            '11月' => ['courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
            '12月' => ['courseSale' => 0, 'courseRefund' => 0, 'productSale' => 0, 'productRefund' => 0, 'productComposeSale' => 0, 'productComposeRefund' => 0, 'total' => 0, 'newStudent' => 0],
        ];

//        $invoices = \app\model\InvoiceDetails::with('proceeds')
//            ->alias('ind')
//            ->join
//            ->join('invoice iv', 'iv.id=ind.invoice_id', 'left')
//            ->where('iv.school_id',$school)
//            ->whereBetween('iv.create_time',$time)
//            ->select();

        $invoices = \app\model\InvoiceDetails::
        alias('ind')
            ->join('proceeds_details pd','invoice_details_id=ind.id','left')
            ->join('invoice iv', 'iv.id=ind.invoice_id', 'left')
            ->where('iv.school_id',$school)
            ->whereBetween('iv.create_time',$time)
            ->select();

        foreach ($invoices as $invoice) {


                $month = date('n', strtotime($invoice->create_time)).'月';

                if ($invoice->type == 1) {

                    $monthData[$month]['productSale'] += $invoice->grandtotal;
                    $monthData[$month]['productRefund'] += RefundDetails::where('cid', $invoice->cid)->where('type', $invoice->type)->value('money');
                    $data['productSaleTotal'] += $invoice->grandtotal;

//                $data['productRefundTotal'] += $monthData[$month]['productRefund'];
                } elseif ($invoice->type == 2) {
                    $monthData[$month]['productComposeSale'] += $invoice->grandtotal;
                    $monthData[$month]['productComposeRefund'] += RefundDetails::where('cid', $invoice->cid)->where('type', $invoice->type)->value('money');
                    $data['productComposeSaleTotal'] += $invoice->grandtotal;
//                $data['productComposeRefundTotal'] += $monthData[$month]['productComposeRefund'];
                } elseif ($invoice->type == 3) {
                    $monthData[$month]['courseSale'] += $invoice->grandtotal;
                    $monthData[$month]['courseRefund'] += RefundDetails::where('cid', $invoice->cid)->where('type', $invoice->type)->value('money');
                    $data['courseSaleTotal'] += $invoice->grandtotal;
//                $data['courseRefundTotal'] += $monthData[$month]['courseRefund'];
                }
            if($invoice->type < 4 ){
                $monthData[$month]['total'] += $invoice->grandtotal;
            }


        }

        foreach ($monthData as $month => $sales) {
            $monthData[$month]['month'] = $month;
            $monthData[$month]['total'] =($monthData[$month]['total']-$monthData[$month]['productRefund']-$monthData[$month]['productComposeRefund']-$monthData[$month]['courseRefund']);
            $data['courseRefundTotal']+=$sales['courseRefund'];
            $data['productRefundTotal'] +=$sales['productRefund'];
            $data['productComposeRefundTotal']+=$sales['productComposeRefund'];
        }
        $data['turnoverTotal'] = array_sum(array_column($monthData, 'total'));
        $data['newStudentTotal'] = 0; // 这里需要计算新学生数量
        $aa['data']=array_values($monthData);
        $aa['to']=$data;
        return $this->success($aa);
    }

}
